﻿using System;
using System.Collections.Generic;
using System.Linq;
using Kyoh.App.Crawler.Plugin;
using Kyoh.Lib.Plugin;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace Kyoh.App.Crawler.Contents
{
    /// <summary>シーカーの管理を行うクラス。</summary>
    class Seekers : PluginManager<ISeeker>
    {
        public Seekers()
        {
        }

        public IEnumerable<string> GetPages(Anchor anchor)
        {
            if (anchor == null)
                return Enumerable.Empty<string>();
            if (string.IsNullOrEmpty(anchor.Uri))
            {
                Trace.TraceError(Properties.Strings.ErrorEmptyAnchor, anchor.Title, anchor.Uri);
                return Enumerable.Empty<string>();
            }

            foreach (var info in this)
            {
                try
                {
                    if (info.Supports(anchor))
                    {
                        var pages = info.GetPages(anchor);
                        if (pages != null)
                            return pages;
                    }
                }
                catch (Exception exc)
                {
                    //TODO:
                    Debug.WriteLine(exc);
                }
            }

            Trace.TraceWarning(Properties.Strings.WarningNotSupportedUri, Properties.Strings.Seeker, anchor.Title, anchor.Uri);
            return Enumerable.Empty<string>();
        }
    }
}
